package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.UserBanquetData;
import com.yanqu.road.entity.banquet.UserBanquetRecord;
import com.yanqu.road.entity.banquet.UserBanquetSetting;
import com.yanqu.road.entity.enums.eBanquetInfoType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBanquetSettingDaoImpl extends TempDataObjectDao<UserBanquetSetting> {

    private static String tableName = "t_u_banquet_setting";

    public Map<Integer, UserBanquetSetting> getUserBanquetSettingMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = String.format("select * from %s where userId = ?", tableName);
        Map<Integer, UserBanquetSetting> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);

            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetSetting data = parse(rs);
                res.put(data.getType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean addData(UserBanquetSetting data) {
        PreparedStatement call = null;
        String sql = String.format("insert into %s(userId,`type`, giftTypeAccept, updateTime, banquetId) values(?,?,?,?,?)", tableName);
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getType());
            call.setString(i++, data.getGiftTypeAccept());
            call.setLong(i++, data.getUpdateTime());
            call.setString(i++, data.getBanquetId());

            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserBanquetSetting data) {
        PreparedStatement call = null;
        String sql = String.format("UPDATE %s SET giftTypeAccept=?,updateTime=?,banquetId=? WHERE userId=? and type=?", tableName);
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getGiftTypeAccept());
            call.setLong(i++, data.getUpdateTime());
            call.setString(i++, data.getBanquetId());

            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getType());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBanquetSetting parseData(ResultSet rs) throws SQLException {
        UserBanquetSetting data = new UserBanquetSetting(
                rs.getLong("userId"),
                rs.getInt("type"),
                rs.getString("giftTypeAccept"),
                rs.getLong("updateTime"));
        data.setBanquetId(rs.getString("banquetId"));
        return data;
    }
}
